/**
 * Created with JetBrains WebStorm. User: Administrator Date: 13-11-12 Time: 上午11:44 To change this template use File |
 * Settings | File Templates.
 */

Ext.define("byb.Constant", {
    statics: {
        AllEmployee: "所有人员",
        APIRoot: "rest",
        LoginURL: "j_security_check",
        LogoutURL: "j_spring_security_logout",
        RandCodeURL:"rest/kaptcha/getKaptchaImage", 
        HRManagerActionURL: "rest/hremployee/",      // for 人资管理专用地址
        AdmDepartmentTypeActionURL: "rest/depType/", // for AdmDepartmentType管理专用地址
        PositionActionURL: "rest/position/",         // for position管理专用地址
        ClassActionURL: "rest/classManager/",        // for class管理专用地址
        AttendanceURL:  "rest/attendance/",          // for attendance管理专用地址
        CalculateURL:  "rest/calculate/",            // for attendance管理专用地址
        AdmCode: null,
        ExceptionServerOperation:"操作提示",
        SheetOperatorCreate: "create",
        // 定义工单的操作类型
        SheetInCode: "SOT_IW",
        SheetOutCode: "SOT_OW",
        SheetAdjustCode: "SOT_AW",
        // 定义工单号的编码类型
        SheetCodeTypeYLR: "YLR",
        SheetCodeTypeYLC: "YLC",
        SheetCodeTypeYLP: "YLP",
        SheetCodeTypeXSR: "XSR",
        SheetCodeTypeXSC: "XSC",
        SheetCodeTypeXSP: "XSP",
        SheetCodeTypeJKR: "JKR",
        SheetCodeTypeJKC: "JKC",
        SheetCodeTypeYHR: "YHR",
        SheetCodeTypeYHC: "YHC",
        LeftMenuSelectedButton: "LeftMenuSelectedButton",
        TabBarOperationAdd: "OperationAdd",
        TabBarOperationDelete: "OperationDelete",
        TabBarOperationModify: "OperationModify",
        TabBarOperationExport: "OperationExport",
        TabBarOperationPrint: "OperationPrint",
        TabBarOperationCancel: "OperationCancel",
        TabBarOperationResetPassword: "OperationResetPassword",
        TabBarOperationExport_Month:"OperationExport_Month",
        TabBarOperationExport_Quarter:"OperationExport_Quarter",
        TabBarOperationExport_Year:"OperationExport_Year",       	 
        TabBarOperationExport_Span:"OperationExport_Span",
        TabBarOperationExport_Sales_Month:"OperationExport_Sales_Month",
        TabBarOperationExport_Sales_Quarter:"OperationExport_Sales_Quarter",
        TabBarOperationExport_Sales_Year:"OperationExport_Sales_Year",       	 
                	
        NXWineType: 'NX_WINE', // 浓香酒标示
        JXWineType: 'JX_WINE', // 酱香酒标示
        SheetCurrentType:"",//工单当前的类型
        CONTRACT_IS_NULL : 0,
        CONTRACT_IS_NORMAL : 1,
        CONTRACT_IS_NEAR : 2,
        CONTRACT_IS_EXPIRED : 3,
        Factory: function(name) {
            if (name == "AdmCode") {
                if (byb.Constant.AdmCode == null) {
                    byb.Constant.AdmCode = Ext.create("byb.AdmCode");
                    return byb.Constant.AdmCode;
                } else {
                    return byb.Constant.AdmCode;
                }
            }
        },
        type: null,
        getType: function(mf) {
            Ext.Ajax.request({
                url: byb.Constant.AdmDepartmentTypeActionURL + '/queryType',
                params: {
                    "codeType": 'Insurances'
                },
                method: 'GET',
                success: function(response, opts) {
                    var obj = Ext.decode(response.responseText);
                    if (obj && obj.hasOwnProperty("results")) {
                        byb.Constant.type = obj.results;
                        if (typeof mf == 'function') mf();
                    }
                }
            });
        },
        checkBalanceOfWarehouse:function(context,model,warehouseId,typeId,number,callback){
            Ext.Ajax.request({
                url: byb.Constant.APIRoot+"/"+model+"/checkBalanceOfWarehouse",
                params: {
                    "model":model,
                    "warehouseId":warehouseId,
                    "typeId":typeId,
                    "number":number
                },
                method: 'GET',
                success: function(response, opts) {
                    var obj = Ext.decode(response.responseText);
                    if(context){
                        if(!obj.results){
                            context.setActiveError("出库数量大于库存数量");
                            context.doComponentLayout();
                        }else{
                            context.clearInvalid();
                        }
                    }
                    if(callback){
                        callback.call(context,obj.results);
                    }
                    return obj.results;
                }
            });
        },
        changeTwoDecimal: function(x) {
            var f_x = parseFloat(x);
            if (isNaN(f_x)) {
                //alert('function:changeTwoDecimal->parameter error');
                return ;
            }
            var f_x = Math.round(x * 100) / 100;
            return f_x;
        }
        
        //判断部门或岗位变换后，是否能编辑转岗日期
        ,xuyiChange: function(ctx, newValue, oldValue, eOpts) {
            if (ctx.up().up().up().up().temp) {
                var orgnizeDesc = ctx.up().down('[name=orgnizeDesc]').getValue();
                var position = ctx.up().down('[name=position]').getValue();
                var origin = ctx.up().up().up().up().temp;
                var obj = Ext.ComponentQuery.query('hrManager_BaseInfo datefield[name=changePDate]')[0];
                if (origin.orgnizeDesc != orgnizeDesc || origin.position != position) {
                    //alert('改了值');
                    obj.setReadOnly(false);
                    obj.allowBlank = false;
                    obj.validate();
                } else {
                    // 改回去了
                    obj.setReadOnly(true);
                    obj.allowBlank = true;
                    obj.validate();
                    obj.setValue(ctx.up().up().up().up().temp.changePDate);
                }
            }
        }
        //判断入职日期变动以后，对转岗日期进行最小值判断
        ,positionChange: function(ctx, newValue, oldValue, eOpts) {
            //入职日期
            var entryDate = new Date(ctx.up().up().down('datefield[name=entryDate]').getValue());
            if (ctx.up().up().up().up().temp) {
                var changePDate = new Date(ctx.up().up().up().up().temp.changePDate);
                var maxTime = Date.parse(changePDate) - Date.parse(entryDate) >= 0 ? changePDate : entryDate;
                //设定转岗最小值
                ctx.up().up().down('datefield[name=changePDate]').setMinValue(maxTime);
            } else {
                ctx.up().up().down('datefield[name=changePDate]').setMinValue(entryDate);
            }
        }
        
        //判断入职和转岗日期变动以后，对离退日期进行最小值判断
        ,leaveChange: function(ctx, newValue, oldValue, eOpts) {
            //入职日期
            var entryDate = ctx.up().down('datefield[name=entryDate]').getValue();
            //转岗日期
            var changeDate = ctx.up().down('datefield[name=changePDate]').getValue();
            var maxTime = Date.parse(changeDate) - Date.parse(entryDate) >= 0 ? changeDate : entryDate;
            if (ctx.up().up().up().up().temp) {
                var d2 = new Date(ctx.up().up().up().up().temp.changePDate);
                var superMaxTime = Date.parse(maxTime) - Date.parse(d2) >= 0 ? maxTime : d2;
                ctx.up().down('datefield[name=leaveDate]').setMinValue(superMaxTime);
            } else {
                ctx.up().down('datefield[name=leaveDate]').setMinValue(maxTime);
            }
        }
        
        ,threeChange: function(ctx, newValue, oldValue, eOpts) {
            var dep = ctx.up().up().down('textfield[name=orgnize]').getValue();
            var pos = ctx.up().up().down('combobox[name=position]').getValue();
            var sts = ctx.up().up().down('combobox[name=employeeStatus]').getValue();
            if (ctx.up().up().up().up().temp) {
                
                // 设定范围
                byb.Constant.positionChange(ctx, newValue, oldValue, eOpts);
                
                var temp = ctx.up().up().up().up().temp;
                var obj = ctx.up().up().down('datefield[name=changePDate]');
                if (temp.orgnize != dep || temp.position != pos || temp.employeeStatus != sts) {
                    obj.setReadOnly(false);
                    obj.allowBlank = false;
                    obj.validate();
                } else {
                    obj.setReadOnly(true);
                    obj.allowBlank = true;
                    obj.validate();
                    obj.setValue(ctx.up().up().up().up().temp.changePDate);
                }
            }
            
        }
        
        //设定转岗日期的minValue
        ,setChangePosDate: function() {
            var window = Ext.ComponentQuery.query("EditTeamWindow")[0];
            var changeObj = window.query("datefield[name=changePDate]")[0];
            var results = window.data.results;
            var maxDate = null;
            for (var i = 0; i< results.length; i++) {
                var r = results[i];
                if (r.changeDate) {
                    var tempDate = new Date(r.changeDate);
                    if (!maxDate) {
                        maxDate = tempDate;
                        continue;
                    } else {
                        maxDate = Date.parse(tempDate) - Date.parse(maxDate) >= 0 ? tempDate : maxDate;
                    }
                }
            }
            if (maxDate) {
                changeObj.setMinValue(maxDate);
            } else {
                changeObj.setMinValue('');
            }
        }
    },
    constructor: function() {

    }


});
